define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Newpro_present, $ionicSlideBoxDelegate, GoPage) {

        //来回切换--1级导航
        $scope.selectTab = 0;
        $scope.activeTab = function(index) {
            $scope.selectTab = index;
            $ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
        };
        $scope.slideChanged = function(index) {
            $scope.selectTab = index;
        };

        //来回切换--2级导航
        $scope.selectTab_second = 0;
        $scope.activeTab_second = function(index) {
            $scope.selectTab_second = index;
            $ionicSlideBoxDelegate.$getByHandle('tab-handle_second').slide(index);
        };
        $scope.slideChanged_second = function(index) {
            $scope.selectTab_second = index;
        };

        $scope.init_data = function() {
            Newpro_present.get_list(1, $scope.selectTab_second + 1, function(data) {
                //console.log(data);
                $scope.data = data;
            });
            Newpro_present.get_jionlist(1, function(data) {
                $scope.list = data;
            });

        }
        $scope.init_data(); //必须在定义之后

        //页面跳转
        $scope.onshow_give = function(id) {
            GoPage.go_states("give", {
                "id": id
            });
        }
        $scope.onshow_save = function() {
            GoPage.go_states("save");
        }
        $scope.onshow_submit = function() {
            GoPage.go_states("submit");
        }
        $scope.onshow_statistics_present = function() {
            GoPage.go_states("statistics_present");
        }

        //下拉刷新
        $scope.input = new Object();
        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
            $scope.isLoad = true;
            if($scope.selectTab == 0) {
                Newpro_present.get_list(1, $scope.selectTab_second + 1, function(data) {
                        console.log(data);
                        if(data != null) {
                            $scope.data = data;

                        } else
                            $scope.data = new Object();
                        $scope.input.nPage = 1;
                    },
                    function() {
                        $scope.$broadcast('scroll.refreshComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);
                    });
            } else {
                Newpro_present.get_jionlist(1, function(data) {
                        console.log(data);
                        if(data != null) {
                            $scope.list = data;

                        } else
                            $scope.data = new Object();
                        $scope.input.nPage = 1;
                    },
                    function() {
                        $scope.$broadcast('scroll.refreshComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);
                    });
            }
        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            if($scope.selectTab == 0) {
                Newpro_present.get_list($scope.input.nPage +1, $scope.selectTab_second + 1, function(data) {
                        console.log(data);
                        if(data != null) {
                            if($scope.data.act != undefined) {
                                $scope.data.act = $scope.data.act.concat(data.act);
                                $scope.input.nPage += 1;
                            } else {
                                $scope.data = data;
                            }

                        }
                    },
                    function() {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);

                    });
            } else {
                Newpro_present.get_jionlist($scope.input.nPage +1, function(data) {
                        //console.log(data);
                        if(data != null) {
                            if($scope.data.act != undefined) {
                                $scope.list.act = $scope.list.act.concat(data.act);
                                $scope.input.nPage += 1;
                            } else {
                                $scope.list = data;
                            }

                        }
                    },
                    function() {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                        $timeout(function() {
                            $scope.isLoad = false;
                        }, 1500);

                    });
            }

        };

    }

    controller.$inject = ['$scope', 'Newpro_present', '$ionicSlideBoxDelegate', 'GoPage'];

    return app.registerController("newpro_presentctrl",controller);
});